<?php

namespace App\Http\Controllers;

use App\Models\ChapterQuestion;
use App\Models\Goods;
use App\Models\GoodsAttr;
use App\Models\LmColumn;
use App\Models\LmSubject;
use App\Models\Smsrecord;
use App\Models\Teacher;
use App\Models\Users;
use App\Models\YtkActivity;
use App\Models\YtkAddress;
use App\Models\YtkCourse;
use App\Models\YtkIntegral;
use App\Models\YtkIntegralTask;
use App\Models\YtkOrders;
use App\Models\YtkSubscribe;
use App\Models\YtkUsers;
use App\Models\YtkUsersCourse;
use App\Models\YtkUsersRebate;
use App\Models\YtkUsersTickets;
use App\Models\YtkVip;
use App\Models\YtkVipLogs;
use App\Models\YtkVipUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Overtrue\EasySms\EasySms;
use Overtrue\EasySms\Exceptions\Exception;
use Yansongda\Pay\Pay;

class PublicController extends Controller
{
    /**
     * 上传文件并返回文件path和url
     * @param Request $request
     * @return array
     */
    public function upload(Request $request)
    {
        //上传文件最大大小,单位M
        $maxSize = 10;
        //支持的上传图片类型
        $allowed_extensions = ["png", "jpg", "gif"];
        //返回信息json
        $data = ['status' => 201, 'msg' => '上传失败', 'data' => ''];
        $file = $request->file('image');

        //检查文件是否上传完成
        if ($file->isValid()) {
            //检测图片类型
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext), $allowed_extensions)) {
                $data['msg'] = "请上传" . implode(",", $allowed_extensions) . "格式的图片";
                return response()->json($data);
            }
            //检测图片大小
            if ($file->getClientSize() > $maxSize * 1024 * 1024) {
                $data['msg'] = "图片大小限制" . $maxSize . "M";
                return response()->json($data);
            }
        } else {
            $data['msg'] = $file->getErrorMessage();
            return response()->json($data);
        }

        if (empty($file)) {
            $return = [
                'status' => 201,
                'msg' => '请选择图片上传'
            ];
        } else {
            $path = date('Y-m-d');
            $public = $request->file('image')->store($path, 'article');
            if ($public) {
                $res = Storage::put($public, Storage::disk('article')->get($public));
                if ($res) {
                    $return = [
                        'status' => 200,
                        'filename' => $public,
                        'msg' => Storage::disk('article')->url($public),
                    ];
                } else {
                    $return = [
                        'status' => 200,
                        'filename' => $public,
                        'msg' => Storage::disk('article')->url($public)
                    ];
                }
            } else {
                $return = [
                    'status_code' => 201,
                    'msg' => trans('上传失败')
                ];
            }
        }
        return $return;
    }

    /**
     * 上传文件并返回文件path和url
     * @param Request $request
     * @return array
     */
    //上传文本文件
    public function uploadtxt(Request $request)
    {
        $file = $request->file('image');
        //上传文件最大大小,单位M
        $maxSize = 300;
        //支持的上传图片类型
        $allowed_extensions = ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf", 'zip', 'rar', 'txt'];
        //检查文件是否上传完成
        if ($file->isValid()) {
            //检测图片类型
            $ext = $file->getClientOriginalExtension();
            if (!in_array(strtolower($ext), $allowed_extensions)) {
                $data['status'] = 201;
                $data['msg'] = "请上传" . implode(",", $allowed_extensions) . "格式的文件";
                return response()->json($data);
            }
            //检测图片大小
            if ($file->getClientSize() > $maxSize * 1024 * 1024) {
                $data['status'] = 201;
                $data['msg'] = "文件大小限制" . $maxSize . "M";
                return response()->json($data);
            }
        } else {
            $data['status'] = 201;
            $data['msg'] = $file->getErrorMessage();
            return response()->json($data);
        }

        if (empty($file)) {
            $return = [
                'status' => 201,
                'msg' => '请选择图片上传'
            ];
        } else {
            $path = date('Y-m-d');
            $FileType = $file->getClientOriginalExtension(); //获取文件后缀
            $FilePath = $file->getRealPath(); //获取文件临时存放位置
            $FileName = $file->getClientOriginalName(); //文件名称
            $FileSize = $file->getClientSize();  //文件大小
            $public = $path . '/' . Str::random(40) . '.' . $FileType;
            Storage::disk('files')->put($public, file_get_contents($FilePath)); //存储文件
            return $data = [
                'status' => 200,
                'ext' => $FileType,
                'title' => $FileName,
                'size' => $FileSize,
                'msg' => "/filepath/" . $public //文件路径
            ];
        }
        return $return;
    }

    /**
     * 上传阿里云文件并返回文件path和url
     * @param Request $request
     * @return array
     */
    public function ossupload(Request $request)
    {
        $file = $request->file('image');
        if (empty($file)) {
            $return = [
                'status' => 201,
                'msg' => '请选择图片上传'
            ];
        } else {
            $path = date('Y-m-d');
            $public = $request->file('image')->store($path, 'oss');
            if ($public) {
                $res = Storage::put($public, Storage::disk('oss')->get($public));
                if ($res) {
                    $return = [
                        'status' => 200,
                        'filename' => $public,
                        'msg' => env('OSS_MYDOMAIN') . $public,
                    ];
                } else {
                    $return = [
                        'status' => 200,
                        'message' => trans('上传成功'),
                        'filename' => $public,
                        'msg' => Storage::disk('oss')->url($public)
                    ];
                }
            } else {
                $return = [
                    'status_code' => 201,
                    'msg' => trans('上传失败')
                ];
            }
        }
        return $return;
    }

}

